﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Utile;

namespace WebApp.admin.user
{
    public partial class userRank : BaseAdmin
    {

    
        tbUserRank oUserRank = new tbUserRank();

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oUserRank.rankName = this.txtRankName.Value;
                this.oUserRank.points = Convert.ToInt32(this.txtPoints.Value);
                this.oUserRank.discount = Convert.ToInt32(this.txtDiscount.Value);
                this.oUserRank.isDefault = MathLibrary.ConvertNumToBool(base.Request.Form["isDefault"]);
             
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oUserRank.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.theUserRank.Update(this.oUserRank))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='userRankList.aspx';</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else if (BaseFactory.theUserRank.Add(this.oUserRank) > 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");window.location.href='userRankList.aspx';</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["ot"] == null)
                {
                    return builder;
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑等级");
                    return builder;
                }
                builder.Append("添加等级");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
            {
                if (base.Request.QueryString["ot"] == "ed")
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.userRankEdit);
                }
                else
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.userRankAdd);
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.UserRankBind();
                }
                else
                {
                    this.rdIsDefault0.Checked = true;
                }
            }
        }

        private void UserRankBind()
        {
            try
            {
                oUserRank = BaseFactory.theUserRank.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (oUserRank != null)
                {
                    this.txtRankName.Value = oUserRank.rankName;
                    this.txtPoints.Value = oUserRank.points.ToString();
                    this.txtDiscount.Value = oUserRank.discount.ToString();
                    if (oUserRank.isDefault)
                    {
                        this.rdIsDefault1.Checked = true;
                    }
                    else
                    {
                        this.rdIsDefault0.Checked = true;
                    }
                }
            }
            catch
            {
            }
        }

    }
}